<script lang="ts" setup>
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { WorkOrderModel } from '#/api/production/models';

import { Page } from '@vben/common-ui';

import { Button, message } from 'ant-design-vue';

import { useVbenVxeGrid } from '#/adapter/vxe-table';
import {
  closeWorkOrderApi,
  getSequenceWorkOrderPagedListApi,
  holdOnWorkOrderApi,
} from '#/api/production/workOrder';

import { useColumns, useSearchSchema } from './data';

const [Grid, gridApi] = useVbenVxeGrid({
  formOptions: {
    collapsed: true,
    schema: useSearchSchema(),
    // 控制表单是否显示折叠按钮
    showCollapseButton: true,
    submitOnEnter: true,
  },
  gridOptions: {
    columns: useColumns(),
    border: true,
    stripe: true,
    pagerConfig: {},
    proxyConfig: {
      ajax: {
        query: async ({ page }, formValues) => {
          return await getSequenceWorkOrderPagedListApi({
            pageNumber: page.currentPage,
            pageSize: page.pageSize,
            ...formValues,
          });
        },
      },
    },
  } as VxeTableGridOptions,
});

/**
 * 暂停或恢复工单
 */
async function onHoldOn(row: WorkOrderModel) {
  await holdOnWorkOrderApi(row.id);
  gridApi.query();
  message.success(
    `工单 ${row.wo} 已${row.status === 'OnHold' ? '恢复' : '暂停'}`,
  );
}

/**
 * 关闭工单
 * @param row
 */
async function onClose(row: WorkOrderModel) {
  await closeWorkOrderApi(row.id);
  gridApi.query();
  message.success(`工单 ${row.wo} 已关闭`);
}
</script>

<template>
  <Page auto-content-height>
    <Grid>
      <template #action="{ row }">
        <Button
          :danger="row.status !== 'OnHold'"
          :disabled="
            !['Scheduled', 'InProgress', 'OnHold', 'Completed'].includes(
              row.status,
            )
          "
          size="small"
          type="link"
          @click="onHoldOn(row)"
        >
          {{ row.status === 'OnHold' ? '恢复' : '暂停' }}
        </Button>
        <Button
          :disabled="row.status !== 'Completed'"
          size="small"
          type="link"
          @click="onClose(row)"
        >
          关闭
        </Button>
      </template>
    </Grid>
  </Page>
</template>
